<?php


namespace app\common\model;

use app\common\model\MobileCode;
use think\Model;
use think\Db;

class Member extends Model
{
    protected $table = "store_member";

    public function register($params)
    {
        if (!empty($params['mobile'])) {
            $find = self::where(['phone' => $params['mobile']])->find();
            if (empty($find)) {
                $row = self::where(['openid' => $params['openid']])->find();
                if (empty($row)) {
                    $result = $this->idcard_check($params['id_card'],$params['username']);
                    $array = json_decode($result,true);
                    if($array['reason']=='成功'){
                        if(strlen($params['openid'])==28){
                            self::insert(['phone' => $params['mobile'], 'username' => $params['username'], 'id_card' => $params['id_card'], 'email' => $params['email'], 'openid' => $params['openid'], 'unionid' => $params['unionid'], 'create_at' => date('Y-m-d H:i:s')]);
                            return 1;
                        }else{
                            return 3;
                        }
                    }else{
                        return 2;
                    }
                }
            }
        }
    }


    public function idcard_check($id_card,$username){
        $host = "https://zidv2.market.alicloudapi.com";
        $path = "/idcheck/Post";
        $method = "POST";
        $appcode = "88e04f763ca740108ca92d405dd92b98";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys = "cardNo=$id_card&realName=$username";
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);

        $result = curl_exec($curl);
        if($result){
            $arr = explode('{"error_code":0,',$result);
            return '{"error_code":0,'.$arr[1];
        }
        return '';
    }


    public function wx_login($params)
    {
        if (!empty($params['openid'])) {
            $find = self::where(['openid' => $params['openid']])->find();
            if ($find) {
                return 1;
            } else {
                Db::table("store_member")->insert(['openid'=>$params['openid'],'create_at'=>date('Y-m-d H:i:s')]);
                return 1;
            }
        } else {
            return '请上传openid';
        }
    }

    public function login($params)
    {
        if (!empty($params['mobile'])) {
            if (!empty($params['code'])) {
                $find = self::where(['phone' => $params['mobile']])->find();
                if ($find) {
                    $MobileCode = new MobileCode();
                    $row = $MobileCode::where(['mobile' => $params['mobile'], 'code' => $params['code']])->order("id desc")->find();
                    if ($row) {
                        return 1;
                    } else {
                        return '验证码错误';
                    }
                } else {
                    return '手机号不存在';
                }
            } else {
                return '请输入验证码';
            }
        } else {
            return '请输入手机号';
        }
    }


    public function user_info($params)
    {
        $find = self::where(['id' => $params['user_id']])->find();
        $find['station'] = Db::table("store_power_station")->where(["user_id"=>$params['user_id']])->find();
        $find['cooperate'] = Db::table("store_member_cooperate")->where(['user_id'=>$params['user_id']])->find();
        $find['tx_count'] = Db::table("store_member_bill")->field("id")->where(['openid'=>$find['openid'],'state'=>'WAIT_USER_CONFIRM'])->count();
        return $find;
    }

    public function apply_cooperate($params)
    {
        $find = Db::table("store_member_cooperate")->field("id")->where(["user_id" => $params['user_id']])->find();
        if (empty($find)) {
            $params['create_at'] = date('Y-m-d H:i:s');
            Db::table("store_member_cooperate")->insert($params);

            $station = Db::table("store_power_station")->field("id")->where(["user_id" => $params['user_id']])->find();
            if (empty($station)) {
                $d['user_id'] = $params['user_id'];
                $d['create_at'] = date('Y-m-d H:i:s');
                Db::table("store_power_station")->insert($d);
            }


        } else {
            $params['create_at'] = date('Y-m-d H:i:s');
            $params['status'] = 0;
            Db::table("store_member_cooperate")->where(["user_id" => $params['user_id']])->update($params);
        }
        return Db::table("store_member_cooperate")->where(["user_id" => $params['user_id']])->find();
    }


    public function find_cooperate($params)
    {
        return Db::table("store_member_cooperate")->where(["user_id" => $params['user_id']])->find();
    }

    public function user_money_log($params)
    {
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;
        $page = ($page - 1) * $limit;
        return Db::table("store_member_amount_log")->where(["user_id" => $params['user_id'],"is_pay"=>1])->order("id desc")->limit($page, $limit)->select();
    }





    public function feedback($params)
    {
        $params['create_at'] = date('Y-m-d H:i:s');
        Db::table("store_feedback")->insert($params);
        return $params;
    }

    public function feedback_list($params)
    {
        return Db::table("store_feedback")->where(["user_id" => $params['user_id']])->order("id desc")->select();
    }


    public function withdrawal($param)
    {
        $member = Db::table("store_member")->where(['id' => $param['user_id']])->find();
        if ($member) {
            if ($param['amount'] > $member['amount']) {
                return '您的余额不足，请及时充值!';
            } else {
                if ($param['amount'] <= 0) {
                    return '请输入大于0的提现金额';
                } else {
                    $new_amount = $member['amount'] - $param['amount'];
                    Db::table("store_member_amount_log")->insert([
                        'user_id' => $member['id'],
                        'amount' => $param['amount'],
                        'before' => $member['amount'],
                        'after' => $new_amount,
                        'memo' => '余额提现',
                        'create_at' => date('Y-m-d H:i:s'),
                        'lx' => 2,
                        'is_pay'=>1,'pay_time'=>date('Y-m-d H:i:s')
                    ]);
                    Db::table("store_member")->where(['id' => $param['user_id']])->update(['amount' => $new_amount]);
                    return 1;
                }
            }
        } else {
            return '您的账号异常，暂不能评价，请联系工作人员！';
        }
    }

    public function my_station($params)
    {
        $find = Db::table("store_power_station")->where(["user_id" => $params['user_id']])->find();
        if($find){
            $find['images'] = str_replace('|',",",$find['images']);
            return $find;
        }else{
            return "";
        }

    }

    public function my_device($params)
    {
        $find = Db::table("store_device")->where(["user_id" => $params['user_id']])->where(['is_shenhe'=>1])->where("delete_time is null")->select();
        if($find){
            foreach ($find as $k =>&$v){
                $v['images'] = str_replace('|',",",$v['images']);
            }
            return $find;
        }else{
            return "";
        }
    }



    public function charge($params)
    {
        $data['user_id'] = $params['user_id'];
        $data['amount'] = $_POST['money'];
        $member = self::where(["id" => $data['user_id']])->find();
        $data['before'] = $member['amount'];
        $data['after'] = $member['amount']+$data['amount'];
        $data['memo'] = '余额充值';
        $data['out_trade_no'] = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['lx'] = 0;

        $data['insert_id'] = Db::table("store_member_amount_log")->insertGetId($data);
        return $data;
    }
}